package com.raceway.ihcenterapp.domain;

import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.validation.constraints.NotNull;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.util.DigestUtils;

@RooJavaBean
@RooToString
@RooEntity(finders = { "findPeopleByUserName" })
public class Person {

    @PrePersist
    @PreUpdate
    protected void encryptPassword() {
        if (password != null && (!password.matches("^[0-9a-fA-F]+$"))) {
            password = DigestUtils.md5DigestAsHex(password.getBytes());
        }
    }

    private String userName;
    private String password;
    private String email;
    private String phoneNumber;

    @NotNull
    @Enumerated(EnumType.STRING)
    private UserRole userRole;

    @ManyToOne
    private IHS workedIHS;
}
